Flask tutorial login problem Part 2

by: glennf, 8 years ago


Thank you for the reply to my last post and for your generosity -  do you make more $$ on hats or shirts?

now to my technical question:



My _init_.py for login is:
def login_page():
    error = ''

    try:
error = None
c, conn = connection()
if request.method == "POST":

data = c.execute("SELECT * FROM tbl_user WHERE user_username = (%s)",
thwart(request.form['username']))

data = c.fetchone()[3]

flash (data)

if sha256_crypt.verify(request.form['password'], data):
session['logged_in'] = True
session['username'] = request.form['username']

flash("You are now logged in")
return redirect(url_for("dashboard"))

else:
error = "(1)Invalid credentials, try again."

gc.collect()

return render_template("login.html", error=error)

    except Exception as e:
#flash(e)
error = "(2)Invalid credentials, try again."
return render_template("login.html", error = error)



I have changed the table name, field name and the number of fetchone field- all verified with the database in MySQL which is:
Table: tbl_user
Columns:
user_id int(11) AI PK
user_name varchar(255)
user_username varchar(255)
user_password varchar(100)

I did the select query from the command line and got the correct row of data.

when I press the login button is get "(2)Invalid credentials, try again." and flash :"not all arguments converted during string formatting "

I had a similar problem in the register section which was remedied by adding the comma after (thwart(username) in the code as seen below:
x = c.execute("SELECT * FROM tbl_user WHERE user_username = (%s)",
                          (thwart(username),))
Not sure exactly why that worked but some site recommended it and it worked.

I have tried that in various ways on the code for login ("data = c.execute("SELECT * FROM tbl_user WHERE user_username = (%s)", thwart(request.form['username']))") but no success.

Any guidance is appreciated.




You must be logged in to post. Please login or register an account.